#include "Date.h"

void Date::Print() const
{
    std::cout << "year:" << _year << " " << "month:" << _month << " " << "day:" << _day << std::endl;
}

int Date::GetMonthDay(int year, int month)
{
    static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && (((year % 100 != 0) && (year % 4 == 0)) || year % 400 == 0)) // 判断闰年
    {
        return days[2] + 1; // 二月加一
    }
    return days[month];
}

// 操作符重载
Date &Date::operator+=(int day)
{
    //考虑负数的情况
    if(day < 0)
    {
        return *this-=(-day);
    }
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        _month++;
        if (_month == 13)
        {
            _month = 1;
            _year++;
        }
    }

    return *this;
}

Date Date::operator+(int day) const
{
    Date tmp = *this;
    return tmp += day;
}

Date &Date::operator-=(int day)
{
    //考虑负数的情况
    if(day < 0)
    {
        return *this+=(-day);
    }
    _day -= day;
    while (_day <= 0)
    {
        _month--;
        if (_month == 0)
        {
            _year--;
            _month = 12;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}

Date Date::operator-(int day) const
{
    Date tmp = *this;
    return tmp -= day;
}

int Date::operator-(const Date &d) const
{
    // 找到它们之间小的那个，假设法
    // 假设d 小于*this
    Date small = d;
    Date big = *this;
    int flag = 1;
    if (d > *this)
    {
        big = d;
        small = *this;
        flag = -1;
    }

    // 方法一：复用
    int cnt = 0;
    while (small != big)
    {
        small += 1;
        cnt++;
    }
    return flag * cnt;
}

// // 操作符重载-比较
bool Date::operator==(const Date &d) const
{
    if ((_year == d._year && _month == d._month) && _day == d._day)
        return true;
    return false;
}
bool Date::operator>(const Date &d) const
{
    if (_year > d._year)
        return true;
    if (_year == d._year)
    {
        if (_month > d._month)
            return true;
        if (_month == d._month)
            return _day > d._day;
    }
    return false;
}
bool Date::operator<=(const Date &d) const
{
    return !(*this > d);
}
bool Date::operator<(const Date &d) const
{
    return !((*this > d) || (*this == d));
}
bool Date::operator>=(const Date &d) const
{
    return !(*this < d);
}
bool Date::operator!=(const Date &d) const
{
    return !(*this == d);
}

// 操作符重载-赋值
// 略-系统生成

// 前置++
Date &Date::operator++()
{
    return *this += 1;
}
// 后置++
Date Date::operator++(int)
{
    Date tmp = *this;
    *this += 1;
    return tmp;
}

// 操作符重载-流插入流提取
// 流插入<<

std::ostream &operator<<(std::ostream &out, const Date &d)
{
    out << d._year << "-" << d._month << "-" << d._day;
    return out;
}

std::istream &operator>>(std::istream &in, Date &d)
{
    // 请输入年月日（以空格分隔）
    in >> d._year >> d._month >> d._day;

    while (!d.Check())
    {
        std::cout << "日期非法，请重试！" << std::endl;
        in >> d._year >> d._month >> d._day;
    }
    return in;
}